package com.lucaswangdev.logindemo.biz.manager.impl;

import com.lucaswangdev.logindemo.biz.manager.ConceptManager;
import com.lucaswangdev.logindemo.constant.common.ErrorConstants;
import com.lucaswangdev.logindemo.exception.BusinessException;
import com.lucaswangdev.logindemo.model.convert.ConceptConverter;
import com.lucaswangdev.logindemo.model.domain.ConceptDO;
import com.lucaswangdev.logindemo.model.domain.ConceptParam;
import com.lucaswangdev.logindemo.model.dto.ConceptDTO;
import com.lucaswangdev.logindemo.model.mapper.ConceptMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ConceptManagerImpl implements ConceptManager {

    @Resource
    private ConceptMapper conceptMapper;

    @Override
    public Long create(ConceptDTO conceptDTO) {
        ConceptDO conceptDO = ConceptConverter.INSTANCE.convertDTO2DO(conceptDTO);
        Date now = new Date();
        conceptDO.setGmtCreate(now);
        conceptDO.setGmtModified(now);
        int effectRow = conceptMapper.insert(conceptDO);
        if (effectRow != 1) {
            throw new BusinessException(ErrorConstants.CREATE_ERROR.getErrorCode(), ErrorConstants.CREATE_ERROR.getErrorMsg());
        }
        return conceptDO.getId();
    }

    @Override
    public Long countByParam(ConceptParam param) {
        return conceptMapper.countByParam(param);
    }

    @Override
    public List<ConceptDTO> queryByParam(ConceptParam param) {
        List<ConceptDTO> conceptDTOS = new ArrayList<>();
        List<ConceptDO> conceptDOS = conceptMapper.selectByParam(param);
        if (CollectionUtils.isNotEmpty(conceptDOS)) {
            conceptDOS.forEach(conceptDO -> {
                conceptDTOS.add(ConceptConverter.INSTANCE.convertDO2DTO(conceptDO));
            });
        }
        return conceptDTOS;
    }

    @Override
    public List<ConceptDTO> queryAllByParam(ConceptParam param) {
        List<ConceptDTO> conceptDTOS = new ArrayList<>();
        List<ConceptDO> conceptDOS = conceptMapper.selectAllByParam(param);
        if (CollectionUtils.isNotEmpty(conceptDOS)) {
            conceptDOS.forEach(conceptDO -> {
                conceptDTOS.add(ConceptConverter.INSTANCE.convertDO2DTO(conceptDO));
            });
        }
        return conceptDTOS;
    }

    @Override
    public void update(ConceptDTO conceptDTO) {
        ConceptDO conceptDO = ConceptConverter.INSTANCE.convertDTO2DO(conceptDTO);
        Date now = new Date();
        conceptDO.setGmtModified(now);
        int effectRow = conceptMapper.updateByPrimaryKeySelective(conceptDO);
        if (effectRow != 1) {
            throw new BusinessException(ErrorConstants.UPDATE_ERROR.getErrorCode(), ErrorConstants.UPDATE_ERROR.getErrorMsg());
        }
    }

    @Override
    public void deleteById(Long id) {
        int effectRow = conceptMapper.deleteByPrimaryKey(id);
        if (effectRow != 1) {
            throw new BusinessException(ErrorConstants.DELETE_ERROR);
        }
    }

    @Override
    public ConceptDTO queryById(Long id) {
        ConceptDO conceptDO = conceptMapper.selectByPrimaryKey(id);
        if (conceptDO == null) {
            throw new BusinessException(ErrorConstants.NOT_FOUND_BY_ID, "id: " + id);
        }
        return ConceptConverter.INSTANCE.convertDO2DTO(conceptDO);
    }
}
